import ImagePicker from 'react-native-image-crop-picker'
import { IS_IOS } from './ScreenUtil'
import { CustomPermissions } from './PermissionsUtil'

let iosPhotoAuthorized = false
let iosCameraAuthorized = false

function PhotoPermissionsHelper () {
  return new Promise((resolve, reject) => {
    console.log(IS_IOS && iosPhotoAuthorized)
    if (IS_IOS && iosPhotoAuthorized) {
      resolve()
    } else {
      CustomPermissions({
        type: 'photo',
        showAlert: true,
        alertConfig: {
          title: '温馨提示',
          subtitle: '人职初需要访问您的相册',
          btnTexts: ['取消', '去开启']
        }
      }).then(() => {
        if (IS_IOS && !iosPhotoAuthorized) {
          iosPhotoAuthorized = true
        }
        resolve()
      })
    }
  })
}

function CameraPermissionsHelper () {
  return new Promise((resolve, reject) => {
    console.log(IS_IOS && iosCameraAuthorized)
    if (IS_IOS && iosCameraAuthorized) {
      resolve()
    } else {
      CustomPermissions({
        type: 'camera',
        showAlert: true,
        alertConfig: {
          title: '温馨提示',
          subtitle: '人职初需要访问您的相机',
          btnTexts: ['取消', '去开启']
        }
      }).then(() => {
        if (IS_IOS && !iosCameraAuthorized) {
          iosCameraAuthorized = true
        }
        resolve()
      })
    }
  })
}

export function openAlums ({
  width = 300,
  height = 300,
  cropping = true,
  mediaType = 'photo'
} = {}) {
  return new Promise((resolve, reject) => {
    PhotoPermissionsHelper()
      .then(() => {
        return ImagePicker.openPicker({
          width,
          height,
          cropping,
          mediaType
        })
      })
      .then(image => {
        if (IS_IOS) {
          image.imgPath = 'file://' + image.path
        } else {
          image.imgPath = image.path
        }
        setTimeout(() => {})
        resolve(image)
      })
      .catch(error => {
        reject(error)
      })
  })
}

export function multipleAlums ({
  mediaType = 'photo',
  maxFiles = 9,
  compressImageMaxWidth = 750,
  compressImageMaxHeight = 750
} = {}) {
  return new Promise((resolve, reject) => {
    PhotoPermissionsHelper()
      .then(() => {
        return ImagePicker.openPicker({
          multiple: true,
          compressImageMaxWidth,
          compressImageMaxHeight,
          compressImageQuality: 0.8,
          mediaType,
          maxFiles
        })
      })
      .then(images => {
        console.log(images)
        images &&
          images.length > 0 &&
          images.forEach(item => {
            if (IS_IOS) {
              item.imgPath = 'file://' + item.path
            } else {
              item.imgPath = item.path
            }
          })
        resolve(images)
      })
      .catch(() => {
        reject()
      })
  })
}

export function openCamera ({ width = 300, height = 300, cropping = true } = {}) {
  return new Promise((resolve, reject) => {
    console.log('CustomPermissions')
    CameraPermissionsHelper()
      .then(() => {
        console.log('ImagePicker.openCamera')
        return ImagePicker.openCamera({
          width,
          height,
          cropping
        })
      })
      .then(image => {
        if (IS_IOS) {
          image.imgPath = 'file://' + image.path
        } else {
          image.imgPath = image.path
        }
        resolve(image)
      })
      .catch(error => {
        reject(error)
      })
  })
}

// export function innerOpenAlums ({width = 300, height = 300, cropping = true, mediaType = 'photo'} = {}) {
//   return new Promise((resolve, reject) => {
//     ImagePicker.openPicker({
//       width,
//       height,
//       cropping,
//       mediaType
//     }).then(image => {
//       if (IS_IOS) {
//         image.imgPath = 'file://' + image.path
//       } else {
//         image.imgPath = image.path
//       }
//       resolve(image)
//     }).catch(error => reject(error))
//   })
// }
//
// function innerMultipleAlums ({mediaType = 'photo'} = {}) {
//   return new Promise((resolve, reject) => {
//     ImagePicker.openPicker({
//       multiple: true,
//       compressImageMaxWidth: 750,
//       compressImageMaxHeight: 750,
//       compressImageQuality: 0.8,
//       mediaType
//     }).then(images => {
//       console.log(images)
//       images && images.length > 0 && images.forEach((item) => {
//         if (IS_IOS) {
//           item.imgPath = 'file://' + item.path
//         } else {
//           item.imgPath = item.path
//         }
//       })
//       resolve(images)
//     }).catch((data) => {
//       reject(data)
//     })
//   })
// }
//
// function innerOpenCamera ({width = 300, height = 300, cropping = true} = {}) {
//   return new Promise((resolve, reject) => {
//     ImagePicker.openCamera({
//       width,
//       height,
//       cropping
//     }).then(image => {
//       if (IS_IOS) {
//         image.imgPath = 'file://' + image.path
//       } else {
//         image.imgPath = image.path
//       }
//       resolve(image)
//     }).catch((error) => {
//       reject(error)
//     })
//   })
// }
//
// function wrapPermissions ({params, subtitle, type} = {
//   subtitle: '人职初需要访问您的相册', type: 'photo'
// }) {
//   return function (fn) {
//     return new Promise((resolve, reject) => {
//       CustomPermissions({
//         type: type || 'photo',
//         showAlert: true,
//         alertConfig: {
//           title: '温馨提示',
//           subtitle: subtitle || '人职初需要访问您的相册',
//           btnTexts: ['取消', '去开启']
//         }
//       }).then(() => {
//         console.log('ImagePicker.openCamera')
//         return fn(params)
//       }).then(image => resolve(image)).catch(err => reject(err))
//     })
//   }
// }
//
// export function openCamera (params) {
//   return wrapPermissions({params, subtitle: '人职初需要访问您的相机', type: 'camera'})(innerOpenCamera)
// }
//
// export function multipleAlums (params) {
//   return wrapPermissions({params})(innerMultipleAlums)
// }
//
// export function openAlums (params) {
//   return wrapPermissions({params})(innerOpenAlums)
//
// }
